package main

import (
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/go-enums/emums"
	"gitee.com/youbeiwuhuan/gin-practice/go-enums/emums/example"
	"github.com/go-playground/locales/en"
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	en_translations "github.com/go-playground/validator/v10/translations/en" // 提示语翻译包
	zh_translations "github.com/go-playground/validator/v10/translations/zh"
)

func InitValidate(local string, validate *validator.Validate) ut.Translator {

	// en 英文，zh 中文
	en := en.New()
	zh := zh.New()
	//初始化翻译器，并指定支持的语种
	uni := ut.New(zh, en, zh)
	// 获取指定语种的翻译器
	trans, _ := uni.GetTranslator(local)

	switch local {
	case "en":
		en_translations.RegisterDefaultTranslations(validate, trans)
	case "zh":
		zh_translations.RegisterDefaultTranslations(validate, trans)
	default:
		zh_translations.RegisterDefaultTranslations(validate, trans)
	}

	//emums.AddEnumValidate(validate, trans)
	return trans
}

type Person struct {
	Hoby string `validate:"enum=HobyEnum"`
}

func enumValid() {
	emums.RegistEnum[string]("HobyEnum", example.HobyEnum)
	v := validator.New()
	ut := InitValidate("zh", v)
	emums.AddEnumValidate(v, ut)
	p := Person{Hoby: "sfdfs"}
	err := v.Struct(p)
	if nil != err {
		validationErrs := err.(validator.ValidationErrors) //断言是ValidationErrors
		for _, validationErr := range validationErrs {
			fmt.Printf("------------%s-----%v---%s--------%s---%v \n", validationErr.Field(), validationErr.Value(), validationErr.Param(), validationErr.StructNamespace(), validationErr.Error()) //返回错误
			fmt.Println(validationErr.Translate(ut))
		}
	}
}

func getEnum() {
	// 必须先注册,否则走到else
	emums.RegistEnum[int]("AnimalsEnum", example.AnimalsEnum)
	if v, ok := emums.GetEnum[int]("AnimalsEnum"); ok {
		fmt.Printf("%+v\n", v)
	} else {
		fmt.Printf("AnimalsEnum doesn't exist\n")
	}

}

func runEnum(e emums.Enum[int]) {
	fmt.Println(e.Description())
	fmt.Printf("----------%+v", e.EnumItems())

	if v, ok := emums.GetEnum[int]("AnimalsEnum"); ok {
		fmt.Printf("%+v\n", v)
	} else {
		fmt.Printf("AnimalsEnum doesn't exist\n")
	}

}

func genEnumCode() {
	cfg := emums.EnumConfig{
		Description: "个人爱好",
		// 文件名称
		FileName: "hoby_enum.go",
		// 值类型
		ValType: "string",
		ValZero: "\"\"",
		// 枚举变量名
		EnumName: "Hoby",
		// 枚举所在的包名
		PackageName: "example",
		// 代码文件输出的路径
		OutPutPath: "E:/tmp",
		NLMap: map[string]string{
			"Football":   "足球",
			"Basketball": "篮球",
			"Badminton":  "羽毛球",
		},
		NVMap: map[string]string{
			"Football":   "\"football\"",
			"Basketball": "\"basketball\"",
			"Badminton":  "\"badminton\"",
		},
	}
	emums.CreateEnum(cfg)
}

func main() {
	//runEnum(example.AnimalsEnum)
	//getEnum()
	//genEnumCode()

	enumValid()

}
